package com.blainpeng.socketcommunicationdemo;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * Created by BlainPeng on 16/5/29.
 */
public class ClientConnector {

    private Socket mClient;
    /**
     * 服务端的ip
     */
    private String mDstName;
    /**
     * 服务端端口号
     */
    private int mDesPort;

    private ConnectLinstener mListener;


    public ClientConnector(String dstName, int dstPort) {
        this.mDstName = dstName;
        this.mDesPort = dstPort;
    }

    /**
     * 与服务端进行连接
     *
     * @throws IOException
     */
    public void connect() {
        try {
            Log.e("connect===", mClient+"==="+mDstName+"==="+mDesPort);

            if (mClient == null) {
                Log.e("connect===1", mClient+"==="+mDstName+"==="+mDesPort);
                mClient = new Socket(mDstName, mDesPort);

//                mClient.getInetAddress().getHostAddress();
            }

            Log.e("connect===2", mClient+"==="+mDstName+"==="+mDesPort);

            //获取其他客户端发送过来的数据
            InputStream inputStream = mClient.getInputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(buffer)) != -1) {
                String data = new String(buffer, 0, len);

                Log.e("connect===3", data+"===");

                //通过回调接口将获取到的数据推送出去
                if (mListener != null) {
                    mListener.onReceiveData(data);
                }
            }
        } catch (Exception e) {
            Log.e("connect===e", e+"===");
            e.printStackTrace();
        }


    }

    /**
     * 认证方法，这个方法是用来进行客户端一对一发送消息的
     * 在实际项目中进行即时通讯时都需要进行登录，这里就是
     * 模拟客户端的账号
     *
     * @param authName
     */
    public void auth(final String authName) throws IOException {

        new Thread(new Runnable(){
            @Override
            public void run() {

                try {
                    if (mClient != null) {
                        Log.e("auth===", mClient+"==="+authName);

                        //将客户端账号发送给服务端，让服务端保存
                        OutputStream outputStream = mClient.getOutputStream();
                        //模拟认证格式，以#开头
                        outputStream.write(("#" + authName).getBytes());


                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();


    }

    /**
     * 将数据发送给指定的接收者
     *
     * @param receiver 信息接数者
     * @param data     需要发送的内容
     */
    public void send(final String receiver, final String data){

        new Thread(new Runnable(){
            @Override
            public void run() {

                try {
                    Log.e("send===", mClient+"==="+receiver+"==="+data);

                    OutputStream outputStream = mClient.getOutputStream();
                    //模拟内容格式：receiver+  # + content
                    outputStream.write((receiver + "#" + data).getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();


    }

    /**
     * 断开连接
     *
     * @throws IOException
     */
    public void disconnect() throws IOException {
        if (mClient != null) {
            mClient.close();
            mClient = null;
        }
    }


    public void setOnConnectLinstener(ConnectLinstener linstener) {
        this.mListener = linstener;
    }

    /**
     * 数据接收回调接口
     */
    public interface ConnectLinstener {
        void onReceiveData(String data);
    }
}
